﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Streambolics.Gui;

namespace Streambolics.Hp41s.DemoApp
{
    public partial class MainForm : Form
    {
        private Engine _Machine = new Engine ();

        private Streambolics.Hp41s.Steps _Program = new Streambolics.Hp41s.Steps (
            new LabelInstruction ("\"MYPROG\""),
            CommonInstructions.SetString ("HELLO WORLD"),
            CommonInstructions.Prompt,
            CommonInstructions.AppendString (", It is me"),
            CommonInstructions.Prompt,
            CommonInstructions.Cla,
            CommonInstructions.Prompt,
            CommonInstructions.Number (10),
            CommonInstructions.Enter,
            CommonInstructions.Number (11),
            CommonInstructions.Plus,
            CommonInstructions.Stop,
            CommonInstructions.Arcl (Reference.X),
            CommonInstructions.Prompt
            );

        public MainForm ()
        {
            InitializeComponent ();
            textBox2.Text = _Program.CompactListing ();
            _Machine.DisplayChanged += delegate (object Sender, EventArgs e)
            {
                textBox1.Text = _Machine.Display;
                Application.DoEvents ();
            };
            mainDisplay.Machine = _Machine;
            stackDisplay.Machine = _Machine;
        }

        private void button1_Click (object sender, EventArgs e)
        {
            Compiler p = new Compiler ();
            MessageBox.Show (p.ToString ());

            Instruction i = new ContextInstruction ((c) => c.DoXeq (_Program, 1), "XEQ \"MYPROG\"");
            ExecAndDisplay (i);

        }

        private void ExecAndDisplay (Instruction i)
        {
            _Machine.Execute (i);
            //textBox1.Text = _Machine.Display;
            textBox2.Text = _Machine.LongListing ();
        }

        private void button_RunStop_Click (object sender, EventArgs e)
        {
            Instruction i = new ContextInstruction ((c) => c.DoResume (), "R/S");
            ExecAndDisplay (i);
        }

        private void button_Inspect_Click (object sender, EventArgs e)
        {
            PropertyDialog d = new PropertyDialog ();
            d.ShowObject (_Machine.Registers);
            d.ShowDialog ();
        }

        private void button_Learn_Click (object sender, EventArgs e)
        {
            Compiler p = new Compiler ();
            p.CompileLine (_Program, textBox1.Text);
            textBox2.Text = _Program.CompactListing ();
        }
    }
}
